package cn.kmsoft.spatialboot.common.object;

import com.alibaba.druid.proxy.jdbc.ClobProxy;
import oracle.sql.CLOB;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.*;

/**
 * @author
 * @date 2018/1/11.
 */
public class OracleClobTypeHandler implements TypeHandler<Object> {
    public Object valueOf(String param) {
        return null;
    }

    @Override
    public Object getResult(ResultSet arg0, String arg1) throws SQLException {
        CLOB clob = (CLOB) arg0.getClob(arg1);
        return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());
    }

    @Override
    public Object getResult(ResultSet arg0, int arg1) throws SQLException {
        return null;
    }

    @Override
    public Object getResult(CallableStatement arg0, int arg1) throws SQLException {
        return null;
    }

    @Override
    public void setParameter(PreparedStatement arg0, int arg1, Object arg2, JdbcType arg3) throws SQLException {
        Clob clob = arg0.getConnection().createClob();
        clob.setString(1, (String) arg2);
        if (clob instanceof ClobProxy) {
            ClobProxy impl = (ClobProxy) clob;
            clob = impl.getRawClob();
        }

        arg0.setClob(arg1, clob);
    }
}
